﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class ProductsList : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdProduct;
                SqlDataReader dtrProduct;
                conn.Open();

                cmdProduct = new SqlCommand("SELECT p_id, p_name, SUBSTRING(p_desc, 1, 50) AS des, p_quantity, p_price, p_picture, p_category, p_views, username FROM Products p, Users u WHERE p.u_id=u.u_id and p_status='Approve'", conn);
                dtrProduct = cmdProduct.ExecuteReader();

                ItemList.DataSource = dtrProduct;
                ItemList.DataBind();

                dtrProduct.Close();
                conn.Close();
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
    }
}